//页面加载等待
/*
 var pageloading = ["loading-img-8"];  
 var n = Math.floor(Math.random() * pageloading.length + 1)-1;  
 var pageclass = pageloading[n];
 var divnum = pageclass.substr(pageclass.length-1,1);
 document.write('<div class="page-loading-box">');
 document.write('<div class="'+pageclass+'">');
 for(var i=0; i< parseInt(divnum); i++){
 document.write('<div></div>');
 }
 document.write('</div></div>');
 document.onreadystatechange = function() {
 if (document.readyState == "complete") {
 $('.page-loading-box').remove();
 }
 }
 */


/*var remote_ip='127.0.0.1';
var remote_port = '8080';
var remote_name='yfcloud/a';*/
var remote_ip='api.yuanfengjishu.com';
var remote_port = '80';
var remote_name='yfcloud/a';
$().ready(function() {
	//远程访问端口地址配置

	var menu = $('#menu');
	var $nav = $('.show-menu-info');
	$(window).on("scroll", function() {
		menu.removeClass('show');
	});

	// 弹出菜单
	$('body').on('click', '.show-menu', function(e) {
		if(e.stopPropagation) {
			e.stopPropagation();
		} else {
			e.cancelBubble = true;
		}
		var bd_top = $(document).scrollTop();
		if(menu.css('opacity') == '0') {
			menu.addClass('show');
		} else {
			menu.removeClass('show');
		}
	});
	// 右侧弹出菜单
	$('.right-menu-btn').click(function() {
		if($(this).parents('.right-menu-box').hasClass('active')) {
			$(this).parents('.right-menu-box').removeClass('active');
		} else {
			$(this).parents('.right-menu-box').addClass('active');
		}
	});
	// 滑动触发
	try {
		document.createEvent("TouchEvent");
		// console.info("支持TouchEvent事件！");
		// 绑定事件
		document.addEventListener('touchmove', function(event) {
			menu.removeClass('show');
		}, false);

		$(document).bind('click', function() {
			menu.removeClass('show');
		});

	} catch(e) {
		// console.info("不支持TouchEvent事件！" + e.message);
		$(document).bind('click', function() {
			menu.removeClass('show');
		});
	}

	// mobile端监听input输入框方法
	$.fn.watch = function(callback) {
		return this.each(function() {
			// 缓存以前的值
			$.data(this, 'originVal', $(this).val());

			if($(this).val() != "" && $(this).parent('.form-control-box').find('.num-clear').size() == 0) {
				if($(this).attr('type') == 'password') {
					$(this).parent('.form-control-box').append('<span class="password-type show-password"></span>');
				}
				$(this).parent('.form-control-box').append('<span class="num-clear"><i class="iconfont">&#xe621;</i></span>');
			}
			// event
			$(this).on('input', function() {
				var originVal = $(this, 'originVal');
				var currentVal = $(this).val();

				if(originVal !== currentVal) {
					$.data(this, 'originVal', $(this).val());
					if(currentVal != '' && $(this).parent('.form-control-box').find('.num-clear').size() == 0) {
						if($(this).attr('type') == 'password') {
							$(this).parent('.form-control-box').append('<span class="password-type show-password"></span>');
						}
						$(this).parent('.form-control-box').append('<span class="num-clear"><i class="iconfont">&#xe621;</i></span>');
					}

					if(currentVal == '' && $(this).parent('.form-control-box').find('.num-clear').size() > 0) {
						$(this).parent('.form-control-box').find('.show-password').remove();
						$(this).parent('.form-control-box').find('.num-clear').remove();
					}
					if($.isFunction(callback)) {
						callback($(this));
					}
				}
			});
		});
	};
	$('body').on('click', '.num-clear', function() {
		$(this).parent('.form-control-box').find('input').val('').focus();
		$(this).prev('.show-password').remove();
		$(this).remove();
	});

	$('body').on('click', '.show-password', function() {
		if($(this).hasClass('on')) {
			$(this).prev('input').attr('type', 'password');
			$(this).removeClass('on');
		} else {
			$(this).prev('input').attr('type', 'text');
			$(this).addClass('on');
		}
	});

	// 重写登录模块
	$.login = {
		// 打开登录对话框
		show: function() {
			// 微商城端直接跳到登录页面
			$.go("/login.html");
		},
		// 登录成功处理函数
		success: function() {
			$.go(window.location.href);
		}
	};

});
//人民币转换相关
var c = "零壹贰叁肆伍陆柒捌玖".split("");
// ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
var _c = {}; // 反向对应关系
for(var i = 0; i < c.length; i++) {
	_c[c[i]] = i;
};

var d = "元***万***亿***万";
var e = ",拾,佰,仟".split(",");

function unit4(arr) {
	var str = "",
		i = 0;
	while(arr.length) {
		var t = arr.pop();
		str = (c[t] + (t == 0 ? "" : e[i])) + str;
		i++;
	}

	str = str.replace(/[零]{2,}/g, "零");

	str = str.replace(/^[零]/, "");
	str = str.replace(/[零]$/, "");
	if(str.indexOf("零") == 0) {
		str = str.substring(1);
	}
	if(str.lastIndexOf("零") == str.length - 1) {
		str = str.substring(0, str.length - 1);
	}

	return str;
}

function _formatD(a) {
	// 转化整数部分
	var arr = a.split(""),
		i = 0,
		result = "";
	while(arr.length) {
		var arr1 = arr.splice(-4, 4);

		var dw = d.charAt(i),
			unit = unit4(arr1);

		if(dw == '万' && !unit) {
			dw = "";
		}
		result = unit + dw + result;
		i += 4;
	}
	return result == "元" ? "" : result;
}

function _formatF(b) {
	// 转化小数部分
	b = b || "";
	switch(b.length) {
		case 0:
			return "整";
		case 1:
			return c[b] + "角";
		default:
			return c[b.charAt(0)] + "角" + c[b.charAt(1)] + "分";
	}
}

function _format(n) {
	var a = ("" + n).split("."),
		a0 = a[0],
		a1 = a[1];
	return _formatD(a0) + _formatF(a1);
}

function parse4(u4) {
	var res = 0;
	while(t = /([零壹贰叁肆伍陆柒捌玖])([拾佰仟]?)/g.exec(u4)) {
		var n = _c[t[1]],
			d = {
				"": 1,
				"拾": 10,
				"佰": 100,
				"仟": 1000
			}[t[2]];
		res += n * d;
		u4 = u4.replace(t[0], "");
	}
	var result = ("0000" + res);
	return result.substring(result.length - 4);
}

function _parseD(d) {
	var arr = d.replace(/[零]/g, "").split(/[万亿]/),
		rs = "";
	for(var i = 0; i < arr.length; i++) {
		rs += parse4(arr[i]);
	};
	return rs.replace(/^[0]+/, "");
};

function _parseF(f) {
	var res = "",
		t = f.replace(/[^零壹贰叁肆伍陆柒捌玖]+/g, "").split(""); // 去掉单位
	if(t.length) {
		res = ".";
	} else {
		return "";
	};
	for(var i = 0;
		(i < t.length && i < 2); i++) {
		res += _c[t[i]];
	};
	return res;
};

function _parse(rmb) {
	var a = rmb.split("元"),
		a1 = a[1],
		a0 = a[0];
	if(a.length == 1) {
		a1 = a0;
		a0 = "";
	}
	return _parseD(a0) + _parseF(a1);

};
//小写转大写
function formatRMB(num) {
	var n = Number(num);
	if(!isNaN(num)) {
		if(num == 0) {
			return "零元整";
		} else {
			return _format(n);
		}
	} else {
		return false;
	}
}
//大写转小写
function parseRMB(rmb) {
	if(/^[零壹贰叁肆伍陆柒捌玖元万亿拾佰仟角分整]{2,}$/.test(rmb)) {
		var result = _parse(rmb);
		return rmb == this.formatRMB(result) ? result : result + "(?)";
	} else {
		return false;
	}
};
//接收地址栏参数
function GetQueryString(name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]);
	    return null;
	}
//返回功能
    function backTo() {
        $(".sb-back").attr("href", "javascript:void(0);");
        if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {
            window.location.href = window.document.referrer;
        } else {
            window.history.go("-1");
        }
    }